<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

$sql = "SELECT user_key, timestamp
FROM serverlog
WHERE (user_key, timestamp)
IN (SELECT user_key, max(timestamp) AS date FROM serverlog GROUP BY user_key)
ORDER BY timestamp DESC";

if ($result =$db->query($sql)) {  
    } else {
      echo "Error: " . $sql . "<br>" . $db->error;
    }

?>
<h2>Server Log</h2>

<div class="container-fluid">
	<div class="row"> 
	<div class="col-md-3"></div>
	<div class="col-md-6">
	<h3>Last Login of Every User</h3>
      
  <table class="table table-hover">
    <thead>
      <tr>
        <th>Username</th>
        <th>Last Login</th>
      </tr>
    </thead>
    <tbody>

<?php
while ($row = $result->fetch_assoc()) {

	$user_key = $row['user_key'];

	$sql2 = "SELECT firstname, lastname 
	FROM users 
	WHERE user_key = '$user_key'";

	$result2 =$db->query($sql2);
	$row2 = $result2->fetch_assoc();

	$firstname = $row2['firstname'];
	$lastname = $row2['lastname'];
	
	$timestamp = $row['timestamp'];
	$timestampconv = strtotime($timestamp);
	$lastlogintime = date("d-m-y H:i", $timestampconv);

	echo "<tr>";
	echo "<td>$firstname $lastname</td>";
	echo "<td>$lastlogintime</td>";
	echo "</tr>";
}

?>  
    </tbody>
  </table>

	</div>
	<div class="col-md-3"></div>

	</div>
		</div>



